<template>
  <div>
    <div class="search">
      <el-form inline label-width="80px" @keyup.enter.native="searchByCondition">
        <el-form-item label="用户名">
          <el-input v-model="params.username" class="search-item">
          </el-input>
        </el-form-item>
        <el-form-item label="状态">
          <DictSelect clearable v-model="params.stateCode" :type-code="$dictConsts.USER_STATE" class="search-item"></DictSelect>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" @click="searchByCondition">查询</el-button>
          <el-button type="primary" size="small" @click="reset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="operate">
      <el-button v-perm="'saveBtn'" type="primary" size="small" class="operate-item" @click="saveUserVisible=true">新增</el-button>
    </div>

    <div class="data" v-loading="loading">
      <el-table :data="data.records">
        <el-table-column prop="username" label="用户名" width="150"> </el-table-column>
        <el-table-column prop="nickname" label="昵称" width="100"> </el-table-column>
        <el-table-column prop="employeeName" label="姓名" width="150"> </el-table-column>
        <el-table-column prop="employeeGenderName" label="性别" width="100"> </el-table-column>
        <el-table-column prop="roleName" label="角色" width="100"> </el-table-column>
        <el-table-column prop="stateName" label="状态" width="150">
          <template slot-scope="scope">
            <el-tag :type="scope.row.stateCode === 1 ? 'success' : 'danger'">
              {{ scope.row.stateName }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" min-width="170" width="auto"> </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="150">
          <template slot-scope="scope">
            <el-button v-perm="'updateBtn'" type="primary" size="mini" @click="showUpdate(scope.row.id)">编辑</el-button>
            <el-popconfirm title="确定要删除吗？" @onConfirm="remove(scope.row.id)">
              <el-button v-perm="'removeBtn'" style="margin-left: 10px;" type="danger" size="mini" slot="reference">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="current"
        :page-sizes="$config.defaultPageSizeOpts"
        :page-size="size"
        layout="total, sizes, prev, pager, next"
        :total="data.total">
      </el-pagination>
    </div>

    <SaveUser :visible="saveUserVisible"
              @on-close="saveUserVisible=false"
              @on-success="saveSuccess">
    </SaveUser>
    <UpdateUser :visible="updateUserVisible"
                :id="id"
                @on-close="updateUserVisible=false"
                @on-success="updateSuccess">
    </UpdateUser>
  </div>
</template>
<script>
import { mapActions } from 'vuex'
import SaveUser from '@/views/system/user/SaveUser'
import UpdateUser from '@/views/system/user/UpdateUser'
import DictSelect from '@/components/form/DictSelect'

export default {
  name: 'User',
  components: { DictSelect, UpdateUser, SaveUser },
  data() {
    return {
      data: {},
      params: {},
      form: {},
      current: 1,
      size: 10,
      saveUserVisible: false,
      updateUserVisible: false,
      id: '',
      loading: false
    }
  },
  methods: {
    ...mapActions(['GetUserPage', 'RemoveUserById']),
    search() {
      this.params.current = this.current
      this.params.size = this.size
      this.loading = true
      this.GetUserPage(this.params).then(res => {
        this.loading = false
        if (res) {
          this.data = res
        }
      }).catch(() => {
        this.loading = false
      })
    },
    reset() {
      this.params = {}
      this.current = 1
    },
    searchByCondition() {
      this.current = 1
      this.search()
    },
    handleSizeChange(val) {
      this.size = val
      this.search()
    },
    handleCurrentChange(val) {
      this.current = val
      this.search()
    },
    saveSuccess() {
      this.search()
      this.saveUserVisible = false
    },
    showUpdate(id) {
      this.id = id
      this.updateUserVisible = true
    },
    updateSuccess() {
      this.search()
      this.updateUserVisible = false
    },
    remove(id) {
      this.RemoveUserById(id).then(() => {
        this.search()
      })
    }
  },
  mounted() {
    this.search()
  }
}
</script>

<style lang="less" scoped>
  .search {
    .search-item {
      width: 200px;
    }
  }
  .operate {
    float: right;
    operate-item {
      padding: 0 10px;
    }
  }
</style>
